package cn.azy.document.tool;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.context.annotation.Scope;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ Author     ：AZY.
 * @ Date       ：Created in 16:07 2019/10/24
 * @ Description：${DESCRIPTION}
 */
@Slf4j
@Scope("prototype")
public class SaveDataToQueue implements Runnable{

    private List<Map<String,String>> list;
    private XWPFTable table;
    private List<String> name;
    private List<String> time;
    private int num;

    public SaveDataToQueue(List<Map<String,String>> list,XWPFTable table,List<String> name,List<String> time,int num)  {
        this.list = list;
        this.table = table;
        this.name = name;
        this.time = time;
        this.num = num;
    }

    @Override
    public void run() {
        List<XWPFTableRow> rows = table.getRows();

        //读取每一行数据
        for (int i = 3; i < rows.size()-3; i++) {
            XWPFTableRow  row = rows.get(i);
            String n = name.get(num);
            String st = "";
            String et = "";
            String day = time.get(num);
            String drp = "";
            //读取每一列数据
            List<XWPFTableCell> cells = row.getTableCells();
            boolean b = false;
            for (int j = 1; j < 6; j++) {
                XWPFTableCell cell = cells.get(j);
                //输出当前的单元格的数据
                String s = cell.getText();
                //如果没有数据，为空行，跳出本行循环
                if(s==null||"".equals(s)){
                    b = true;
                    break;
                }
                //处理word每行的数据，格式化
                if (j == 1) {
                    st = day + s.trim();
                } else if (j == 2) {
                    if(s.equals("24:00")){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date d = null;
                        try {
                            d = sdf.parse(st);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar c = Calendar.getInstance();
                        c.setTime(d);
                        c.add(Calendar.DAY_OF_YEAR,1);
                        st = sdf.format(c.getTime())+" 00:00";
                    }else{
                        st = st +" "+ s.trim();
                    }
                } else if (j == 3) {
                    if(s.equals("1")){
                        et = day + s.trim();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date d = null;
                        try {
                            d = sdf.parse(et);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar c = Calendar.getInstance();
                        c.add(Calendar.MONTH,1);
                        et = sdf.format(d);
                    }else{
                        et = day + s.trim();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date d = null;
                        try {
                            d = sdf.parse(et);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        et = sdf.format(d);
                    }
                } else if (j == 4) {
                    if(s.equals("24:00")){
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date d = null;
                        try {
                            d = sdf.parse(et);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        Calendar c = Calendar.getInstance();
                        c.setTime(d);
                        c.add(Calendar.DAY_OF_YEAR,1);
                        et = sdf.format(c.getTime())+" 00:00";
                    }else{
                        et = et +" "+ s.trim();
                    }
                } else if (j == 5) {
                    drp = s.trim();
                }
            }
            //行为空，没有数据，跳出此表循环
            if(b){
                break;
            }
            synchronized (this) {
                Map<String, String> map = new HashMap<>();
                map.put("name", n);
                map.put("st", st);
                map.put("et", et);
                map.put("drp", drp);

                list.add(map);
            }

        }
    }
}
